﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Kyoh.App.Renamer
{
    //HACK:http://dobon.net/vb/dotnet/control/tsrenderer.html
    public partial class ToolStripIndexedMenuItem : ToolStripMenuItem
    {
        public ToolStripIndexedMenuItem() : base() { }
        public ToolStripIndexedMenuItem(string text) : base(text) { }
        public ToolStripIndexedMenuItem(string text, EventHandler onClick) : base(text, null, onClick) { }
        public ToolStripIndexedMenuItem(string text, params ToolStripItem[] dropDownItems) : base(text, null, dropDownItems) { }
        public ToolStripIndexedMenuItem(string text, EventHandler onClick, Keys shortcutKeys) : base(text, null, onClick, shortcutKeys) { }
        public ToolStripIndexedMenuItem(string text, EventHandler onClick, string name) : base(text, null, onClick, name) { }

        public int Index { get; set; }

        ToolStripRenderer _renderer;
        protected override void OnOwnerChanged(EventArgs e)
        {
            if (_renderer != null)
            {
                try { _renderer.RenderItemImage -= OnRenderItemImage; }
                catch { }
                try { Image.Dispose(); }
                catch { }
            }
            if (Owner != null)
            {
                _renderer = Owner.Renderer;
                if (_renderer != null)
                {
                    _renderer.RenderItemImage += OnRenderItemImage;
                    Image = new Bitmap(Owner.ImageScalingSize.Width, Owner.ImageScalingSize.Height);
                }
            }
            base.OnOwnerChanged(e);
        }

        protected override Point DropDownLocation
        {
            get
            {
                return new Point(base.DropDownLocation.X, (int)(base.DropDownLocation.Y - SystemInformation.MenuHeight * (DropDownItems.Count / 2.3)));
            }
        }

        private void OnRenderItemImage(object sender, ToolStripItemImageRenderEventArgs e)
        {
            if (e.Item == this)
            {
                var text = (Index+1).ToString();
                var font = new Font(new FontFamily("Verdana"), Font.Size, Font.Style);
                var size = e.Graphics.MeasureString(text, font);
                
                using (var icon = new Bitmap(Math.Max(e.ImageRectangle.Width, (int)(size.Width+0.5F)), Math.Max(e.ImageRectangle.Height, (int)(size.Height+0.5F)), PixelFormat.Format32bppArgb))
                using (var graph = Graphics.FromImage(icon))
                {
                    graph.TextRenderingHint |= System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                    graph.DrawString(text, font, SystemBrushes.MenuText, Point.Empty);
                    e.Graphics.DrawImage(icon, e.ImageRectangle);
                }
            }
        }
    }
}
